﻿@page "/gateway/variable"
@namespace ThingsGateway.Gateway.Razor
@using ThingsGateway.Admin.Application
@using ThingsGateway.Admin.Razor
@using ThingsGateway.Gateway.Application
@attribute [Authorize]
@attribute [RolePermission]
@inherits ComponentDefault


<div style="height: var(--table-height);padding: 2px 5px 10px 5px;">

    <AdminTable @ref=table TItem="Variable" AutoGenerateColumns="true" ShowAdvancedSearch=true
                AllowResizing="true" IsFixedHeader=true IsMultipleSelect=true CustomerSearchModel="@CustomerSearchModel" SearchMode=SearchMode.Top
                ShowExtendButtons=true ShowExportButton
                ShowDefaultButtons=true ExtendButtonColumnWidth=150
                OnQueryAsync="OnQueryAsync" IsPagination=true
                OnSaveAsync="Save" OnDeleteAsync="Delete">

        <CustomerSearchTemplate>
            @if (context is VariableSearchInput model)
            {
                @Render(model)
            }
        </CustomerSearchTemplate>

        <TableColumns>

            <TableColumn @bind-Field="@context.DeviceId" Align="Alignment.Center">
                <Template Context="deviceId">
                    @{
                        CollectDeviceDict.TryGetValue(deviceId.Value!.Value, out var device);
                    }
                    @device?.Name
                </Template>

            </TableColumn>
        </TableColumns>
        <EditTemplate Context="context">
            <VariableEditComponent Model=context BusinessDevices="BusinessDeviceNames" CollectDevices="CollectDeviceNames" BusinessDeviceDict=BusinessDeviceDict CollectDeviceDict=CollectDeviceDict></VariableEditComponent>

        </EditTemplate>


        <ExportButtonDropdownTemplate Context="ExportContext">
            <Button class="dropdown-item" OnClick="() => ExcelExportAsync(ExportContext)" IsDisabled=@(!AuthorizeButton("导出"))>
                <i class="fas fa-file-export"></i>
                <span>@DefaultLocalizer["TablesExportButtonExcelText"]</span>
            </Button>
            <Button class="dropdown-item" OnClick="() => ExcelImportAsync(ExportContext)" IsDisabled=@(!AuthorizeButton("导入"))>
                <i class="fas fa-file-import"></i>
                <span>@DefaultLocalizer["TablesImportButtonExcelText"]</span>
            </Button>
        </ExportButtonDropdownTemplate>
        <TableToolbarTemplate>

            <TableToolbarButton TItem="Variable" IsDisabled=@(!AuthorizeButton(AdminOperConst.Edit))
                                Color=Color.Info Text="@DefaultLocalizer["BatchEdit"]"
                                OnClickCallback=@(BatchEdit) />


            <TableToolbarPopConfirmButton TItem="Variable"
                                          Color=Color.Warning Text="@Localizer["Clear"]" IsDisabled=@(!AuthorizeButton("清空"))
                                          IsAsync OnConfirm=@(async()=>{await Task.Run(async()=> { await VariableService.ClearVariableAsync();
                                          await InvokeAsync(async()=>
                                          {
                                          await ToastService.Default(); await table.QueryAsync();
                                          });
                                          });} ) />

            <PopConfirmButton TItem="Variable"
                              Color=Color.Warning Text="@Localizer["Test"]" IsDisabled=@(!AuthorizeButton("测试"))
                              IsAsync OnConfirm=@(async()=>{await Task.Run(async()=> {await VariableService.InsertTestDataAsync(TestCount);
                              await InvokeAsync(async()=>
                              {
                              await ToastService.Default(); await table.QueryAsync();
                              });
                              });} )>

                <BodyTemplate>
                    <BootstrapInput @bind-Value=TestCount ShowLabel="true" ShowLabelTooltip="true" />
                </BodyTemplate>

            </PopConfirmButton>

        </TableToolbarTemplate>
    </AdminTable>
</div>

@code {
    AdminTable<Variable> table;
}
@code {
    RenderFragment Render(VariableSearchInput model) =>
    @<div class="row form-inline g-2">
        <div class="col-12 col-md-6">
            <Select IsVirtualize @bind-Value="model.DeviceId" Items="@(new List<SelectedItem>() {   new SelectedItem(string.Empty, "none") }.Concat(CollectDeviceNames))" ShowSearch="true" ShowLabel="true" IsClearable DisableItemChangedWhenFirstRender=true />
        </div>
        <div class="col-12 col-md-6">
            <BootstrapInput @bind-Value="model.Name" ShowLabel="true" />
        </div>
        <div class="col-12 col-md-6">
            <BootstrapInput @bind-Value="model.RegisterAddress" ShowLabel="true" />
        </div>
    </div>;

}
